import { isLoginChose } from "./m-login"
import { comPortApi, comPort } from "./m-comPort"


// 微信录音管理
class AudioManager{
    constructor(){
        this.time = 0
        this.timeText = '0:00'
    }

    startRecord(){//开始录音
        const that = this
        that.time = 0
        that.timeText = '0:00'
        clearInterval(that.timeInterval)

        // 显示正在录音，VUE未完成

        wx.startRecord()
        that.onVoiceRecordEnd()
        that.timeInterval=setInterval(() => {
            if(that.time === 60){
                clearInterval(that.timeInterval)
                return
            }
            that.time += 1
            that.timeText = that.returnTimeToStyle(that.time)
            $('#recording-time-span').text(that.timeText)
        },1000)
    }

    endRrcord(){ // 停止录音
        console.log('停止录音')
        const that = this
        clearInterval(this.timeInterval);
        wx.stopRecord({// 停止录音
            success: function (res) {
                that.id = res.localId
                that.uploadVoice()
            }
        })
        // $('#imageluyin').removeClass('imageluyin-start')
        // $('#audio-div-recording').addClass('disnone')
    }

    uploadVoice(){// 上传录音
        //调用微信的上传录音接口把本地录音先上传到微信的服务器
        //不过，微信只保留3天，而我们需要长期保存，我们需要把资源从微信服务器下载到自己的服务器
        const ticket = etCookie('Ticket')

        if(isLoginChose(ticket)){
            const that=this;
            wx.uploadVoice({
                localId: that.id, // 需要上传的音频的本地ID，由stopRecord接口获得
                isShowProgressTips: 1, // 默认为1，显示进度提示
                success: function (res) {
                    //把录音在微信服务器上的id(res.serverId)发送到自己的服务器供下载。
                    const postData = {
                        'Ticket': ticket,
                        'Media_id': res.serverId,
                        'fileType': 'amr',
                        'outfileType': 'mp3'
                    }
                    $.ajax({
                        type: 'post',
                        url: comPortApi + '/api/v1/Donload',
                        data: postData,
                        success: function (res, status) {
                            if (status == "success") {
                                console.log('上传录音 res',res)
                                if (res.data != undefined && res.data != null) {
                                    that.audioUrl = res.data
                                    const id1 = that.id.split('/')
                                    that.id = id1[id1.length - 1]
                                    that.loadAudio()
                                }
                            }
                        },
                        error: function (res, status) {
                            console.log(status)
                            console.log(res)
                        }
                    })
                }
            })
        }
    }

    loadAudio(){
        const that = this
        new Promise(resolve=>{
            const audio = new Audio()
            audio.src = comPort + that.audioUrl
            audio.addEventListener('canplaythrough', that.pushAudio())
        })
    }

    pushAudio(){ // append录音
        
    }

    returnTimeToStyle(time){ // 以0:00形式返回录音时长,time秒数
        if (time >= 60) {
            return '01:00'
        } else {
            //得到
            if(time < 10){
                time = '0'+time
            }
            return `0:${time}`
        }
    }

    onVoiceRecordEnd(){ // 监听是否超过60秒
        const that = this
        wx.onVoiceRecordEnd({
            // 录音时间超过一分钟没有停止的时候会执行 complete 回调
            complete: function (res) {
                clearInterval(that.timeInterval)
                that.id = res.localId
                that.uploadVoice()
            }
        })
    }
}

export { AudioManager }